package com.gewu.pm.ui.activity.order;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.gewu.pm.R;
import com.gewu.pm.app.AppActivity;
import com.gewu.pm.http.glide.GlideApp;
import com.gewu.pm.other.IntentKey;
import com.gewu.pm.ui.adapter.ImageBannerAdapter;
import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品详情页
 */
public final class GoodsDetailsActivity extends AppActivity {

    private Banner mBanner;
    private TextView mGoodsTitleView;
    private TextView mGoodsCodeView;
    private TextView mGoodsTypeView;
    private TextView mGoodsBrandView;
    private TextView mGoodsBarcodeView;
    private TextView mGoodsShelfLifeView;
    private ImageView mDetailImageView;

    /**
     * 启动商品详情页
     * 
     * @param context 上下文
     * @param goodsId 商品ID（可选）
     * @param goodsName 商品名称
     */
    public static void start(Context context, String goodsId, String goodsName) {
        Intent intent = new Intent(context, GoodsDetailsActivity.class);
        intent.putExtra(IntentKey.ID, goodsId);
        intent.putExtra(IntentKey.TITLE, goodsName);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_goods_details;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mBanner = findViewById(R.id.banner);
        mGoodsTitleView = findViewById(R.id.tv_goods_title);
        mGoodsCodeView = findViewById(R.id.tv_goods_code);
        mGoodsTypeView = findViewById(R.id.tv_goods_type);
        mGoodsBrandView = findViewById(R.id.tv_goods_brand);
        mGoodsBarcodeView = findViewById(R.id.tv_goods_barcode);
        mGoodsShelfLifeView = findViewById(R.id.tv_goods_shelf_life);
        mDetailImageView = findViewById(R.id.iv_detail_image);

        // 设置轮播图
        setupBanner();

        // 加载底部详情图片
        GlideApp.with(this)
            .load("https://cdn.pixabay.com/photo/2015/12/01/20/28/road-1072823_960_720.jpg")
            .into(mDetailImageView);
    }

    @Override
    protected void initData() {
        // 获取 Intent 参数
        String goodsId = getString(IntentKey.ID);
        String goodsName = getString(IntentKey.TITLE);

        // 设置商品标题
        if (goodsName != null) {
            mGoodsTitleView.setText(goodsName);
            setTitle(goodsName);
        }

        // TODO: 根据 goodsId 加载商品详情数据
        loadMockData();
    }

    /**
     * 设置轮播图
     */
    private void setupBanner() {
        // 轮播图图片列表
        List<String> imageList = new ArrayList<>();
        imageList.add("https://static.wikia.nocookie.net/onepiece/images/6/6d/Monkey_D._Luffy_Anime_Post_Timeskip_Infobox.png/revision/latest?cb=20141008161929");
        imageList.add("https://static.wikia.nocookie.net/onepiece/images/5/52/Roronoa_Zoro_Anime_Post_Timeskip_Infobox.png/revision/latest?cb=20141008162043");
        imageList.add("https://static.wikia.nocookie.net/onepiece/images/6/68/Nami_Anime_Post_Timeskip_Infobox.png/revision/latest?cb=20141008162415");
        imageList.add("https://static.wikia.nocookie.net/onepiece/images/b/b6/Sanji_Anime_Post_Timeskip_Infobox.png/revision/latest?cb=20141008162600");

        // 设置适配器
        ImageBannerAdapter adapter = new ImageBannerAdapter(imageList, this);
        mBanner.setAdapter(adapter);

        // 设置指示器（页码）
        mBanner.setIndicator(new CircleIndicator(this));

        // TODO: 设置指示器位置到左下角
        // 需要查找 banner 框架的相关方法
    }

    /**
     * 加载模拟数据
     */
    private void loadMockData() {
        mGoodsCodeView.setText("101103002");
        mGoodsTypeView.setText("豆腐（生鲜）");
        mGoodsBrandView.setText("祖名");
        mGoodsBarcodeView.setText("2223411111");
        mGoodsShelfLifeView.setText("冷藏8天");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 释放 Banner 资源
        if (mBanner != null) {
            mBanner.destroy();
        }
    }
}

